﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Job.Core
{
    public class SessionHelper
    {
        public static object GetSession(string strSessionName)
        {
            return System.Web.HttpContext.Current.Session[strSessionName] as object;
        }

        /// <summary>
        /// 获取SESSION
        /// </summary>
        /// <param name="strSessionName"></param>
        /// <param name="blDecrypt">当此值为true时，务必保证SESSION中的值为字符串</param>
        /// <returns></returns>
        public static object GetSession(string strSessionName, bool blDecrypt)
        {
            if (System.Web.HttpContext.Current.Session != null)
            {
                if (System.Web.HttpContext.Current.Session[strSessionName] != null)
                    return blDecrypt ? SecretHelper.Decrypt(System.Web.HttpContext.Current.Session[strSessionName].ToString(), ConfigHelper.GetConfigValue("SecretKey")) : System.Web.HttpContext.Current.Session[strSessionName] as object;
                else
                    return new object();
            }
            else
                return new object();
        }

        /// <summary>
        /// 写SESSION
        /// </summary>
        /// <param name="strSessionName"></param>
        /// <param name="obj">写入的值为一个object</param>
        public static void SetSession(string strSessionName, object obj)
        {
            System.Web.HttpContext.Current.Session[strSessionName] = obj;
        }

        /// <summary>
        /// 写Session
        /// </summary>
        /// <param name="strSessionName"></param>
        /// <param name="strValue"></param>
        /// <param name="blNeedEncrypt">true=加密 false=不加密</param>
        public static void SetSession(string strSessionName, string strValue, bool blNeedEncrypt)
        {
            System.Web.HttpContext.Current.Session[strSessionName] = blNeedEncrypt ? SecretHelper.Encrypt(strValue, ConfigHelper.GetConfigValue("SecretKey")) : strValue;
        }

        public static void RemoveSession(string strSessionName)
        {
            System.Web.HttpContext.Current.Session[strSessionName] = null;
        }
    }
}
